package common

import (
	"common/json_response"
	"common/zapx"
	"gateway/internal/logic"
	"gateway/internal/types/v1/common"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CommonController struct {
	logic *logic.CommonLogic
}

func NewCommonController() *CommonController {
	return &CommonController{
		logic: logic.NewCommonLogic(),
	}
}

// UploadFile 上传文件
func (cc *CommonController) UploadFile(context *gin.Context) {
	response := json_response.NewJsonHttpResponse(context)
	var req common.UploadFileRequest
	if err := context.ShouldBind(&req); err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	f, err := req.File.Open()
	if err != nil {
		zapx.Logger.Error("操作上传的文件失败", zap.Error(err))
		response.SetCode(json_response.CODE_ERROR).SetMsg("系统错误").Send()
		return
	}
	res, err := logic.UploadFile(req.File.Filename, req.File.Header.Get("Content-Type"), f)
	if err != nil {
		response.SetCode(json_response.CODE_ERROR).SetMsg(err.Error()).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(common.UploadFileResponse{
		FilePath: res.Key,
		FullUrl:  res.Uri,
	}).Send()
	return
}

func (cc *CommonController) Init(ctx *gin.Context) {
	response := json_response.NewJsonHttpResponse(ctx)
	data, err := cc.logic.GetConfig(
		"customer_service_text",
		"mac_download_url",
		"windows_download_url",
		"android_download_url",
		"ios_download_url",
	)
	if err != nil {
		response.SetMsg(err.Error()).SetCode(json_response.CODE_ERROR).Send()
		return
	}
	response.SetCode(json_response.CODE_SUCCESS).SetData(data).Send()
	return
}
